<template>
  <div class="login">
    <div class="login_logo">
      <img src="" alt="" />
    </div>
    <div class="title">武汉理工大学</div>
    <div class="title">重大疾病医疗互助基金管理系统</div>
    <!-- <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <div class="logo_title">欢迎登录</div>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      >
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="
            width: 300px;
            height: 48px;
            background: #0d72e4;
            border-radius: 6px;
          "
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'"
            >立即注册</router-link
          >
        </div>
      </el-form-item>
    </el-form> -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <div class="tabheader">
        <div
          class="unify"
          @click="clickStatus = true"
          :class="[clickStatus ? 'now' : '']"
        >
          统一身份认证
        </div>
        <div
          class="tablogin"
          @click="clickStatus = false"
          :class="[!clickStatus ? 'now' : '']"
        >
          账号密码登录
        </div>
      </div>
      <div class="loginform" v-if="!clickStatus">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <div
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
          <!-- <el-button @click="handleLogin" type="text">账号密码登录</el-button> -->
        </div>
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%"
            class="otherbtn"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'"
              >立即注册</router-link
            >
          </div>
        </el-form-item>
      </div>
      <div class="loginform" v-if="clickStatus">
        <div class="unifybox">
          武汉理工大学重大疾病医疗互助基金管理系统所有用户的用户名和密码等账户信息均已同步至统一身份认证系统，请在使用本系统服务前进行身份认证。
        </div>
        <el-button class="otherbtn" @click="handleOtherLogin" link
          >前往统一身份认证系统</el-button
        >
      </div>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2023</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg, ssoCasLogin } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { setToken } from "@/utils/auth";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      clickStatus: true,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
        /*console.log(route.query, "ticket");
        if (route.query.ticket) {
          //this.getToken();
          //window.location.href="http://127.0.0.1:8083/sso/casLogin";
        }*/
        if (route.query.token) {
          setToken(route.query.token);
          this.$router.push("/");
        }
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },

  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleOtherLogin() {
      // ssoCasLogin().then((res) => {
      //   console.log(res);
      // });
      // https://dbhzgl.whut.edu.cn/sso/casLogin
      const path =
        //"http://zhlgd.whut.edu.cn/tpass/login?service=http://localhost:80/login";
        "http://127.0.0.1:8083/sso/casLogin";

      location.href = path;
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
    getToken() {
      ssoCasLogin().then((res) => {
        console.log(res, "token");
      });
    },
    // 单点登录
    checkLogin() {
      if (this.$route.query.token) {
        this.$store.dispatch("dandianLogin", this.$route.query.token);
        this.$router.push("/");
      }
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  // justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100%;
  //background-image: url("");
  background-size: cover;
}
.login_logo {
  margin: 105px 0 16px;
}
.title {
  font-size: 32px;
  font-weight: 500;
  color: #2f3034;
  line-height: 48px;
  // margin: 0px auto 30px auto;
  // text-align: center;
  // color: #707070;
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.now {
  background: linear-gradient(180deg, #d5eaff 0%, #ffffff 100%);
  border-radius: 10px 10px 0px 0px;
  color: #007fff;
  height: 56px;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  height: 427px;
  margin-top: 10px;
  background: #ffffff;
  box-shadow: 0px 0px 10px 2px rgba(222, 226, 232, 0.8);
  border-radius: 10px;
  box-sizing: border-box;
  .tabheader {
    display: flex;
    font-size: 18px;
    align-items: center;
    width: 400px;
    height: 56px;
    background: linear-gradient(180deg, #ffffff 0%, #f7f8fa 100%);
    border-radius: 10px 10px 0px 0px;
    font-weight: bold;
    color: #353535;
    line-height: 24px;
    .unify,
    .tablogin {
      width: 200px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }
  }
  // padding: 25px 25px 5px 25px;
  .el-input {
    height: 48px;
    input {
      height: 48px;
    }
  }
  .input-icon {
    height: 48px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 80px;
  line-height: 24px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #959595;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
.loginform {
  padding: 40px 50px 72px;
  .unifybox {
    width: 300px;
    height: 144px;
    background: #f2f2f6;
    border-radius: 8px;
    padding: 24px;
    font-size: 14px;
    text-align: center;
    color: #567895;
    line-height: 24px;
  }
  .otherbtn {
    width: 300px;
    height: 48px;
    border-radius: 6px;
    font-size: 16px;
    display: flex;
    background: #007fff;
    align-items: center;
    justify-content: center;
    line-height: 22px;
    margin-top: 40px;
    color: #ffffff;
    cursor: pointer;
  }
}
.el-form-item {
  margin-bottom: 16px !important;
}
</style>
